import 'package:flutter_styled_toast/flutter_styled_toast.dart';
import 'package:flutter/material.dart';

class ToastUtils {
  static DateTime? _lastClickTime;

  static void showCenterToast(BuildContext context, String message) {
    final now = DateTime.now();
    if (_lastClickTime != null && now.difference(_lastClickTime!).inSeconds < 2) {
      return;
    }
    _lastClickTime = now;
    showToast(
      message,
      context: context,
      position: StyledToastPosition.center,
      animation: StyledToastAnimation.scale,
      reverseAnimation: StyledToastAnimation.fade,
      duration: Duration(seconds: 2),
      animDuration: Duration(milliseconds: 300),
      curve: Curves.easeInOut,
    );
  }

  static ToastFuture? _loadingToastFuture;

  static void showLoadingToast(BuildContext context) {
    _loadingToastFuture = showToastWidget(
      Container(
        width: 100,
        height: 100,
        decoration: BoxDecoration(
          color: Colors.black.withValues(alpha: 0.5),
          borderRadius: BorderRadius.circular(15),
        ),
        child: const Padding(
          padding: EdgeInsets.all(40.0),
          child: CircularProgressIndicator(
            strokeWidth: 2,
            valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
          ),
        ),
      ),
      context: context,
      position: StyledToastPosition.center,
      animation: StyledToastAnimation.scale,
      reverseAnimation: StyledToastAnimation.fade,
      duration: Duration(seconds: 30),
      animDuration: Duration(milliseconds: 300),
      curve: Curves.easeInOut,
    );
  }

  static void dismissLoadingToast() {
    _loadingToastFuture?.dismiss();
    _loadingToastFuture = null;
  }
}